/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.iti.jets.gpts.service;

import com.iti.jets.gpts.home.TranServiceHome;
import com.iti.jets.gpts.home.TranServiceTypeHome;
import com.iti.jets.gpts.pojo.TranService;
import com.iti.jets.gpts.pojo.TranServiceRoute;
import com.iti.jets.gpts.pojo.TranServiceType;
import java.io.Serializable;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import org.hibernate.Hibernate;

/**
 *
 * @author U
 */
@Path("/Service")
public class TranServiceTypeService implements Serializable {

    TranServiceTypeHome dao = new TranServiceTypeHome(util.HibernateUtil.getSessionFactory());
    TranServiceHome daoTranservice = new TranServiceHome(util.HibernateUtil.getSessionFactory());

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/all")
    public JSONArray getAllAttribute() throws JSONException {

        List<TranServiceType> result = dao.loadAll();
        JSONArray jsonTypeArray = new JSONArray();

        for (TranServiceType tranServiceType : result) {
            JSONObject jSONObject = new JSONObject();
            jSONObject.put("name", tranServiceType.getName());
            jSONObject.put("id", tranServiceType.getId());
            jsonTypeArray.put(jSONObject);
        }
        return jsonTypeArray;
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.TEXT_PLAIN)
    @Path("/allbyid")

    public JSONArray getAllServicesByID(@QueryParam("id") int id) {

        List<TranService> result = daoTranservice.getAllTranserviceByID(id);
        JSONArray jsonTypeArray = new JSONArray();
        for (TranService tranService : result) {
            try {
                JSONObject jSONObject = new JSONObject();
                jSONObject.put("name", tranService.getName());
                jSONObject.put("id", tranService.getId());
                jSONObject.put("terminal1name", tranService.getTerminalByTerminal1().getPoint().getName());
                jSONObject.put("terminal2name", tranService.getTerminalByTerminal2().getPoint().getName());
                jSONObject.put("terminal1id", tranService.getTerminalByTerminal1().getPoint().getId());
                jSONObject.put("terminal2id", tranService.getTerminalByTerminal2().getPoint().getId());
                jsonTypeArray.put(jSONObject);
            } catch (JSONException ex) {
                ex.printStackTrace();
            }
        }
        return jsonTypeArray;
    }

    @Path("/gettsr/{id}")
    @Produces(MediaType.APPLICATION_JSON)
    @GET
    public JSONArray gettsr(@PathParam(value = "id") int id) throws JSONException {
        TranService read = daoTranservice.read(id);
        Hibernate.initialize(read);
        JSONArray jSONArray = new JSONArray();
        Iterator iterator = read.getTranServiceRoutes().iterator();
        while (iterator.hasNext()) {
            TranServiceRoute tsr = (TranServiceRoute) iterator.next();
            JSONObject jSONtsr = new JSONObject();
            jSONtsr.put("id", tsr.getId());
            jSONtsr.put("awayorder", tsr.getAwayOrder());
            jSONtsr.put("homeorder", tsr.getHomeOrder());
            JSONObject jSONpoint = new JSONObject();
            jSONpoint.put("id", tsr.getPoint().getId());
            jSONpoint.put("name", tsr.getPoint().getName());
            jSONpoint.put("location", tsr.getPoint().getLocation());
            jSONpoint.put("is_stop", tsr.getPoint().getIsStop());
            jSONtsr.put("point", jSONpoint);
            jSONArray.put(jSONtsr);
        }
        return jSONArray;
    }
  
}
